<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Nine
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Nine/Model.php';
class Models_Transaction extends Nine_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'transaction_id';
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'transaction';
        return parent::__construct($config); 
    }
    
    /**
     * Get transaction by id
     * 
     * @param int $id
     * @return Zend_Db_Table_Row
     */
    public function getTransactionById( $id ) 
    {
    	$select = $this->select()
    			->where('transaction_id=?',$id);
    	
    	return $this->fetchRow($select);
    }
    
    public function getTransactionWithUser($id) 
    {
    	$select = $this->select()
    			->setIntegrityCheck(false)
    			->from(array('t' => $this->_name))
    			->join(array('u1' => $this->_prefix . "user"),'t.from_user_id = u1.user_id', array('from_name' => 'full_name','from_email' => 'email'))
    			->join(array('u2' => $this->_prefix . "user"),'t.to_user_id = u2.user_id', array('to_name' => 'full_name','to_email' => 'email'))
    			->where('transaction_id=?',$id);
    	
    	return $this->fetchAll($select)->current()->toArray();
    }
    
    
    public function getAllTransaction($condition = array(), $order = null, $count = null, $offset = null) 
    {
    	$select = $this->select()
    			->setIntegrityCheck(false)
    			->from(array('ts' => $this->_name))
    			->join(array('u1' => $this->_prefix . "user"),'ts.from_user_id = u1.user_id', array('from_name' => 'full_name','from_email' => 'email'))
    			->join(array('u2' => $this->_prefix . "user"),'ts.to_user_id = u2.user_id', array('to_name' => 'full_name','to_email' => 'email'))
    			->where($this->getAdapter()->quoteInto('ts.from_user_id = ? OR ts.to_user_id = ?', Nine_Registry::getLoggedInUserId()))
    			->order($order)
    			->limit($count,$offset);
    	
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getTransactionReportPerMonthByUserId($userId)
    {
    	$startTime = $this->_getTimeAtBeforeMonth();
    	$endTime = time();
    	
    	$select = $this->select()
    			->setIntegrityCheck(false)
    			->from(array('ts' => $this->_name))
    			->join(array('u1' => $this->_prefix . "user"),'ts.from_user_id = u1.user_id', array('from_name' => 'full_name','from_email' => 'email'))
    			->join(array('u2' => $this->_prefix . "user"),'ts.to_user_id = u2.user_id', array('to_name' => 'full_name','to_email' => 'email'))
    			->where($this->getAdapter()->quoteInto('ts.from_user_id = ? OR ts.to_user_id = ?', $userId))
    			->where('ts.created_date > ?',$startTime)
    			->where('ts.created_date < ?',$endTime)
    			->where('ts.approved = 1')
    			->order('ts.transaction_id DESC');
    			
    	return $this->fetchAll($select)->toArray();
    }
    
 	private function _getUnixTimeAtBeginMonth()
    {
    	$month = date('m',time());
    	$year = date('Y',time());
    	$day = 1;
    	
    	return mktime(0,0,0,$month,$day,$year);
    	
    }
    
	private function _getTimeAtBeforeMonth()
	{
		/**
		 * Get current
		 */
		$current = getdate();
		
		$beforeMonth = mktime(0,0,0,$current['mon'] - 1,$current['mday'] ,$current['year']);
		return $beforeMonth;
	}
    
}